package com.d0319;

public class EggsTest2 {
    volatile boolean hasEggs=false;//提供同步变量的设置
    Object flag=new Object();
    Thread human=new Thread(()->{
        while (true){
            if(!hasEggs){
                System.out.println("等待");
                synchronized (flag) {
                    try {
                        flag.wait();//阻塞
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }else{
                System.out.println("收取");
                hasEggs=false;
            }
        }
    });
    Thread human2=new Thread(()->{
        while (true){
            if(!hasEggs){
                System.out.println("等待2");
                synchronized (flag) {
                    try {
                        flag.wait();//阻塞
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }else{
                System.out.println("收取2");
                hasEggs=false;
            }
        }
    });

    Thread hen=new Thread(()->{
        while(true){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            hasEggs=true;
            synchronized (flag){
            flag.notify();//唤醒
           }
        }
    });

    public static void main(String[] args) {
        EggsTest2 eggsTest=new EggsTest2();
        eggsTest.hen.start();
        eggsTest.human.start();
        eggsTest.human2.start();
    }
}
